/**
 * Created by WT on 2017/4/5.
 */
$(function () {
    var PROJECT_SPECIALTY = CONSTANT.DictionaryTypeDefConstants.PROJECT_SPECIALTY;//项目专业类别PROJECT_SPECIALTY
    var PROJECT_OPERATIONSTATE = CONSTANT.DictionaryTypeDefConstants.PROJECT_OPERATIONSTATE;//项目当前状态
    queryComboData(PROJECT_SPECIALTY, 'querySpecialtye');
    queryComboData(PROJECT_OPERATIONSTATE, 'queryOperationState');
    $('#btn_save').on('click', function () {
        submitForm();
    });
    //选择项目
    $('#projectName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                addPro();
            }
        }]
    });
    //当前状态为“停止建设”时，停止原因可编辑
    $('#queryOperationState').combobox(
        {
            onSelect: function (record) {
                if (record.text != "停止建设") {
                    $('#stoppageReasons').textbox('setValue', '');
                    $('#stoppageReasons').textbox('textbox').attr("disabled", "disabled").addClass('textbox-disabled');
                } else {
                    $('#stoppageReasons').textbox('textbox').removeAttr("disabled").removeClass('textbox-disabled');
                }
            }
        }
    )
});
function addPro() {
    WindowManager.openLayer({
        type: 2,
        title: '选择项目',
        area: ['800px', '500px'],
        content: ROOT_PATH + '/threeSimultaneities/projectInfo/projectQueryList.shtml?isMulti=1',
        callBack:getSelectedProject
    });
}
//带入项目信息
var getSelectedProject = function (data) {
    var check=checkProject(data[0].id);
    if(check){
        $('#projectName').textbox('setValue', data[0].name);//项目名称
        $('#projectId').val(data[0].id);//项目ID
        $('#projectLevel').textbox('setValue', data[0].projectLevel);//项目级别
        $('#supervisionLevel').textbox('setValue', data[0].supervisionLevel);//监管级别
        $('#deptName').textbox('setValue', data[0].deptName);//所属部门
        $('#projectType').textbox('setValue', data[0].projectType);//项目类别
        $('#investQuota').textbox('setValue', data[0].investQuota);//投资额度
    }else{
        return;
    }
}
function submitForm() {
    var specialNames=$('#querySpecialtye').combobox('getText');
    $('#specialtyNames').val(specialNames);
    $('#projectInfo').form('submit', {
        url: ROOT_PATH + '/threeSimultaneities/projectApproval/add.shtml',
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    WindowManager.closeLayer({
                        returnValue: true
                    });
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}
/**
 * 查询下拉列表
 * @param code
 * @param queryId
 */
function queryComboData(code, queryId) {
    var pageId = '#' + queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            var multiple;
            //项目专业可多选
            if(queryId=='querySpecialtye'){
                    multiple=true;
            }else{
                   multiple=false;
            }
            $(pageId).combobox({
                data: result,
                prompt:'请选择',
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                multiple:multiple
            });

        }
    });
}
/**
 * 验证所选项目是否已存在
 */
 function checkProject(projectId) {
            var checkR=$.ajax({
                async : false,
                cache : false,
                type : 'post',
                url : ROOT_PATH+'/threeSimultaneities/projectApproval/selectByProjectId.shtml',
                data : {
                    id:projectId
                }
            }).responseText;
            var dataObj=$.parseJSON(checkR);//转换为json对象
            var flag=dataObj.success==true;
            if(!flag){
                layer.alert('项目信息已存在!');
            }
            return flag;
        }